#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mapbridge.h"

#include <QWebChannel>
#include <QWebEngineView>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_mapBridge = new MapBridge(this);
    m_channel = new QWebChannel(this);
    m_channel->registerObject(QString("bridge"), m_mapBridge);
    m_wgtMapView = new QWebEngineView(ui->wgtMap);
    connect(m_wgtMapView, &QWebEngineView::loadFinished, this, &MainWindow::slotMapLoadFinished);
    loadMap();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slotMapLoadFinished(bool state)
{
    if(state)
    {
        m_mapBridge->addMapsTips(87.404, 43.915, QString::fromStdString("项目1"), "aaaa", 1);
        m_mapBridge->addMapsTips(109.404, 38.915,QString::fromStdString("项目2"), "bbbb", 1);
    }
}

void MainWindow::loadMap()
{
    m_wgtMapView->setContextMenuPolicy(Qt::NoContextMenu);
    QUrl url= QUrl::fromLocalFile("/onlineMap/map.html");
    if(url.isValid())
    {
        m_wgtMapView->page()->load(url);
    }
    ui->mapLayout->addWidget(m_wgtMapView);
    m_mapBridge->setViewPtr(m_wgtMapView);
}

